package com.jeremyaburns.oddio.tracker 
{
	import com.jeremyaburns.oddio.Instrument;
	import com.jeremyaburns.oddio.Song;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	// Shows a list of the instruments in the current song.  Allows the
	// user to bring up the OTWaveEditor on each instrument, or create
	// new tracks with a selected instrument as the source.
	internal class OTInstrumentList extends Sprite
	{
		static public const EVENT_CLICK_TILE :String = "eventclicktile";
		
		public var song :Song;
		
		private var tiles :Vector.<Sprite>;
		
		public function OTInstrumentList( song:Song ) 
		{
			this.song = song;
			
			graphics.beginFill( OTStyle.instrumentPanelBGColor, 1 );
			graphics.drawRect( 0, 0, OTStyle.instrumentPanelNumTiles*OTStyle.instrumentTileWidth + (OTStyle.instrumentPanelNumTiles+1)*OTStyle.instrumentPanelPadding, 200 );
			graphics.endFill();
			
			tiles = new Vector.<Sprite>;
			var newTile :Sprite = new Sprite();// = generateInstrumentTile( song.instruments[0] );
			tiles.push( newTile );
			newTile.x = OTStyle.instrumentPanelPadding + OTStyle.instrumentTileWidth  /2;
			newTile.y = OTStyle.instrumentPanelPadding + OTStyle.instrumentTileHeight /2;
			newTile.buttonMode = true;
			newTile.addEventListener( MouseEvent.CLICK, tile_click );
			drawTile( 0 );
			addChild( newTile );
		}
		
		private function tile_click( e:MouseEvent ) :void
		{
			dispatchEvent( new OTEvent( EVENT_CLICK_TILE, tiles.indexOf( e.target ) ) );
		}
		
		public function dispose() :void
		{
			
		}
		
		public function refreshTile( index:int ) :void
		{
			drawTile( index );
		}
		
		public function resize( height:Number ) :void
		{
			graphics.clear();
			graphics.beginFill( OTStyle.instrumentPanelBGColor, 1 );
			graphics.drawRect( 0, 0, OTStyle.instrumentPanelNumTiles*OTStyle.instrumentTileWidth + (OTStyle.instrumentPanelNumTiles+1)*OTStyle.instrumentPanelPadding, height );
			graphics.endFill();
		}
		
		private function drawTile( index:int ) :void
		{
			var tile:Sprite = tiles[ index ];
			var instrument:Instrument = song.instruments[ index ];
			
			tile.graphics.clear();
			
			// Draw backdrop.
			tile.graphics.beginFill( OTStyle.instrumentTileBGColor, 1 );
			tile.graphics.drawRect( -OTStyle.instrumentTileWidth/2, -OTStyle.instrumentTileHeight/2, OTStyle.instrumentTileWidth, OTStyle.instrumentTileHeight );
			tile.graphics.endFill();
			
			// Draw waveform.
			tile.graphics.lineStyle( OTStyle.instrumentTileLineSize, OTStyle.instrumentTileFGColor );
			var x0:Number = -OTStyle.instrumentTileWidth/2 + OTStyle.instrumentTilePadding;
			var dx:Number = (OTStyle.instrumentTileWidth - 2*OTStyle.instrumentTilePadding) / instrument.waveform.length;
			tile.graphics.moveTo( x0 , instrument.waveform[0]*OTStyle.instrumentTileHeight/2 );
			for( var i:int = 0 ; i < instrument.waveform.length ; ++i ) {
				tile.graphics.lineTo( x0 + i*dx , instrument.waveform[i]*OTStyle.instrumentTileHeight/2 );
			}
		}
	}
}